package com.chen.blog.controller;

import com.chen.blog.pojo.Article;
import com.chen.blog.service.ArticleService;
import com.chen.blog.utils.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cgj
 * @date 1/20/25 11:03 AM
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
@Tag(name ="文章模块")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping("/showAll")
    public Result showAll(@RequestBody Article article){
        List<Article> articleList = articleService.showAllByArticle(article);
        return Result.ok(articleList);
    }
    @GetMapping("/{id}")
    public Result showArticle(@PathVariable("id") Integer id){
        Article article = articleService.showArticleById(id);
        return  Result.ok(article);
    }
    @GetMapping("showAllByUid/{uid}")
    public Result showArticleByUid(@PathVariable Integer uid){
//        路径参数名字一样可以不用指定
        List<Article> articleList = articleService.showArticleByUid(uid);
        return  Result.ok(articleList);
    }
    @PutMapping
    public Result changeArticle(@RequestBody Article article){
        Integer row = articleService.changeArticle(article);
        if(row>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @PostMapping
    public Result  addArticle(@RequestBody Article article){
        Integer row = articleService.addArticle(article);
        if(row>0) {
            return Result.ok();
        }else {
            return  Result.fail();
        }
    }
    @DeleteMapping("/{id}")
    public Result removeArticle(@PathVariable Integer id){
        Integer result = articleService.removeArticleById(id);
        if(result>0){
            return  Result.ok();
        }else {
            return  Result.fail();
        }
    }
}
